<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
   @media screen and (min-width:400px) {
        body{
            margin: 0px;
            padding: 0px;
            background-image: url("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.puchedu.cn%2Fuploads%2F1%2F26%2F1116940351%2F872024376.jpg&refer=http%3A%2F%2Fimg.puchedu.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1660222921&t=bf319f8cee47ba94d5ac9d13adf4cfbe");
            background-position: 50% 35% ;
            background-repeat: no-repeat;
            background-size: 60%;
        }
        .box{
            width: 22vw;
            margin: 150px auto;
            border: 1px solid ;
            padding: 20px;
            padding-bottom: 30px;
            border-radius: 20px;
            background-color: rgba(255, 255, 255, 0.97);
        }
        .but{
            height: 4vw;
            width:9vw;
            padding: 0px;
            border: 0px;
            margin: 10px;
            overflow: hidden;
            border-radius: 20px;
            font-size: 30px;
        }
        #A{
            background-color: rgba(255, 0, 0, 0.574);
        }
        #B{
            background-color: rgba(0, 0, 255, 0.577);
        }
        #show div{
            font-size: 20px;
            margin: 10px;
        }
        #show div:last-child{
            margin-bottom: 40px;
        }
        #clear{
            width:70px;
            height: 30px;
            float: right;
            margin-top: -20px;
            margin-right: 30px;
            border-radius: 7px;
            border: 0px;
            background-color: rgb(225, 220, 220); 
        }
        .A{
            color: rgba(255, 0, 0, 0.503);
        }
        .B{
            color: rgba(0, 0, 255, 0.505);
        }
    }
    @media screen and (max-width:400px) {
        body{
            margin: 0px;
            padding: 0px;
            background-image: url("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.puchedu.cn%2Fuploads%2F1%2F26%2F1116940351%2F872024376.jpg&refer=http%3A%2F%2Fimg.puchedu.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1660222921&t=bf319f8cee47ba94d5ac9d13adf4cfbe");
            background-position: 50% 50% ;
            background-repeat: no-repeat;
            background-size: 130%;
        }
        .box{
            width: 40vw;
            margin: 220px auto;
            border: 1px solid ;
            padding: 20px;
            padding-bottom: 30px;
            border-radius: 20px;
            background-color: rgba(255, 255, 255, 0.97);
        }
        .but{
            height: 17vw;
            width:14vw;
            padding: 0px;
            border: 0px;
            margin: 10px;
            overflow: hidden;
            border-radius: 20px;
            font-size: 30px;
        }
        #A{
            background-color: rgba(255, 0, 0, 0.574);
        }
        #B{
            background-color: rgba(0, 0, 255, 0.577);
        }
        #show div{
            font-size: 20px;
            margin: 10px;
        }
        #show div:last-child{
            margin-bottom: 40px;
        }
        #clear{
            width:70px;
            height: 30px;
            float: right;
            margin-top: -30px;
            margin-right: 0px;
            border-radius: 7px;
            border: 0px;
            background-color: rgb(225, 220, 220); 
        }
        .A{
            color: rgba(255, 0, 0, 0.503);
        }
        .B{
            color: rgba(0, 0, 255, 0.505);
        }
    }
    </style>
</head>
<body>
    <!-- 按钮 -->
    <div class="box">
        <div>
            <button id="A" class="but">A</button>
            <button id="B" class="but">B</button>
        </div>
        <div id="show">
            <div>
                样本容量:<span>0</span><br>
            </div>
            <div>
                A% :<span class="A">0</span>
            </div>
            <div>
                B% :<span class="B">0</span>
            </div>
        </div>
        <button id="clear">清零</button>
    </div>
</body>

<script crossorigin="anonymous" src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
    var countA=countB=sum=0,isSending = false,correctA,correctB

    const A = document.getElementById('A')
    const B = document.getElementById('B')
    const cl = document.getElementById('clear')
    const show = document.getElementsByTagName("span")

    const xhr = new XMLHttpRequest()
    

    if(sessionStorage.getItem("sum") !== null){
        show[0].innerHTML= sessionStorage.getItem("sum")
        show[1].innerHTML= toPercent(sessionStorage.getItem("correctA"))
        show[2].innerHTML= toPercent(sessionStorage.getItem("correctB"))
    }

    function toPercent(num){
        // var str = Number(num*100).toFixed(2)
        var str = Number(num*100).toFixed(0)
        str += "%"
        return str;
    }
    // 统计A
    A.addEventListener('click',function(){
        countA = Number(sessionStorage.getItem("countA"))+1
        countB = Number(sessionStorage.getItem("countB"))
        if (isSending) xhr.abort()
        isSending = true
        $.ajax({
            type:"GET",
            url:"http://81.68.100.215:8080/count/save",
            data:{
                A:countA,
                B:countB
            },
            dataType:"text",
            jsonp: 'callback', 
            headers: {
                "Content-Type": "application/json; charset=utf-8"
            },
            success:function(data){
                $.ajax({
                    type:"GET",
                    url:"http://81.68.100.215:8080/count/get",
                    dataType:"json",
                    jsonp: 'callback', 
                    headers: {
                        "Content-Type": "application/json; charset=utf-8"
                    },
                    success:function(data){
                        sum =data[0].counta+data[0].countb
                        correctA = data[0].passEndA
                        correctB = data[0].passEndB
                        sessionStorage.setItem("sum",sum)
                        sessionStorage.setItem("countA",data[0].counta)
                        sessionStorage.setItem("countB",data[0].countb)
                        sessionStorage.setItem("correctA",correctA)
                        sessionStorage.setItem("correctB",correctB)
                        show[0].innerHTML = sum
                        show[1].innerHTML = toPercent(correctA)
                        show[2].innerHTML = toPercent(correctB)
                    }
                })
            }
        });  
    })
    // 统计B
    B.addEventListener('click',function(){
        countA = Number(sessionStorage.getItem("countA"))
        countB = Number(sessionStorage.getItem("countB"))+1
        if (isSending) xhr.abort()
        isSending = true
        $.ajax({
            type:"GET",
            url:"http://81.68.100.215:8080/count/save",
            data:{
                A:countA,
                B:countB
            },
            dataType:"text",
            jsonp: 'callback', 
            headers: {
                "Content-Type": "application/json; charset=utf-8"
            },
            success:function(){
                $.ajax({
                    type:"GET",
                    url:"http://81.68.100.215:8080/count/get",
                    dataType:"json",
                    jsonp: 'callback', 
                    headers: {
                        "Content-Type": "application/json; charset=utf-8"
                    },
                    success:function(data){
                        sum =data[0].counta+data[0].countb
                        correctA = data[0].passEndA
                        correctB = data[0].passEndB
                        sessionStorage.setItem("sum",sum)
                        sessionStorage.setItem("countA",data[0].counta)
                        sessionStorage.setItem("countB",data[0].countb)
                        sessionStorage.setItem("correctA",correctA)
                        sessionStorage.setItem("correctB",correctB)
                        show[0].innerHTML = sum
                        show[1].innerHTML = toPercent(correctA)
                        show[2].innerHTML = toPercent(correctB)
                    }
                })
        
            }
        });
    })

 // 清除
 cl.addEventListener("click",function(){
       if(confirm("是否清除记录")){
            $.ajax({
                type:"GET",
                url:"http://81.68.100.215:8080/count/save",
                dataType:"json",
                jsonp: 'callback', 
                headers: {
                    "Content-Type": "application/json; charset=utf-8"
                }
            })
            countA = countB =0    
            show[0].innerHTML=show[1].innerHTML=show[2].innerHTML = 0  
            sessionStorage.clear();

       }
    })
</script>
</html>